package com.yanqu.road.server.gameplayer.module.specialcookboy;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.specialcookboy.config.SpecialCookBoyConfig;
import com.yanqu.road.entity.activity.specialcookboy.config.SpecialCookBoyLevelConfig;
import com.yanqu.road.entity.activity.specialcookboy.config.SpecialCookBoyPromptConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.specialcookboy.LogSpecialCookBoyFinishLevel;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.specialcookboy.ShareCodeUtil;
import com.yanqu.road.entity.specialcookboy.SpecialCookBoyUserData;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.pb.activity.RecallPlayerProto;
import com.yanqu.road.pb.specialcookboy.SpecialCookBoyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.recallplayer.RecallPlayerMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.TaskMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.specialcookboy.SpecialCookBoyMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;

public class SpecialCookBoyModule extends GeneralModule {

    private SpecialCookBoyUserData userData;

    public SpecialCookBoyModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        if (SpecialCookBoyMgr.getActivityInfo() == null) {
            return true;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return true;
        }

        userData = SpecialCookBoyMgr.getOrCreateUserData(getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    public void syncPosterQRCode(){
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        SpecialCookBoyProto.SpecialCookBoyPosterQRCodeSync.Builder builder = SpecialCookBoyProto.SpecialCookBoyPosterQRCodeSync.newBuilder();
        builder.setIsDisplay(ChannelConfig.SPECIAL_COOK_BOY_DISPLAY_QR_CODE_TYPE);
        String appId = player.getAppId();
        String resultQrCode = "";
        if (StringUtils.isNullOrEmpty(appId)){
            appId = "default";
        }

        JSONObject dataObj  = JSONObject.parseObject(ChannelConfig.SPECIAL_COOK_BOY_QR_CODE);
        if (dataObj != null){           //解析出来不为空
            String dataObjString = dataObj.getString(appId);
            if (dataObjString != null){             //有这个key才赋值
                resultQrCode = dataObjString;
            }
        }

        builder.setQRCode(resultQrCode);
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_QR_CODE_SYNC,builder);
    }


    /**
     * 引导记录（记录从1开始）
     * @returns
     * 1：CG动画及创角
     * 2：引导商铺及对话1
     * 3：云雾过场进入第一关 第一关内部引导（江峰）
     * 4：点击钱庄
     * 5：云雾过场进入第二关 第二关内部引导（恒辉）
     * 6：引导对话
     */

    public int finishGuide(List<Integer> steps) {
        if (userData == null){
            if (SpecialCookBoyMgr.getActivityInfo() != null) {
                userData = SpecialCookBoyMgr.getOrCreateUserData(getUserId());
            }
        }

        if (userData == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }
        SpecialCookBoyConfig config = SpecialCookBoyMgr.getConfig();
        if (config == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }
        SpecialCookBoyProto.SpecialCookBoyFinishGuideRespMsg.Builder respMsg = SpecialCookBoyProto.SpecialCookBoyFinishGuideRespMsg.newBuilder();
        respMsg.setRet(0);
        Property guideReward = new Property();
        for (Integer step : steps) {
            if (isFinishGuide(step)) {
                continue;
            }
            // 判断是否有奖励

            // 记录引导
            userData.setGuideBit(userData.getGuideBit() | (1 << (step - 1)));
        }
        if (!guideReward.isNothing()) {
            player.getModule(CurrencyModule.class).addCurrency(guideReward, eLogMoneyType.SpecialCookBoy, eLogMoneyType.SpecialCookBoyGuideReward);
            respMsg.setReward(PropertyHelper.parsePropertyToString(guideReward));
        }
        respMsg.setGuideBit(userData.getGuideBit());
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_FINISH_GUIDE, respMsg);
        return 0;
    }

    /**
     * 是否完成引导
     */
    public boolean isFinishGuide(int step) {
        if (userData == null) {
            return false;
        }
        return ((userData.getGuideBit() >> (step - 1) & 1) == 1);
    }


    @Override
    public void loginSendMsg() {
        syncPosterQRCode();
        syncConfig();
        checkLevelUnlock();
        checkShareCodeUseCountReset();
        updateTaskValue();
        syncUserData();
    }

    @Override
    public boolean saveData() {
        return true;
    }

    /**
     * 同步配置
     *
     * @return
     */
    public void syncConfig() {
        if (SpecialCookBoyMgr.getActivityInfo() == null) {
            return;
        }
        SpecialCookBoyProto.SpecialCookBoyConfigSync.Builder configMsg = SpecialCookBoyMgr.getConfigMsg(player.getLanguage());
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_CONFIG_SYNC, configMsg);
    }

    /**
     * 同步用户数据
     */
    public void syncUserData() {
        if (SpecialCookBoyMgr.getActivityInfo() == null) {
            return;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return;
        }

        if (userData == null) {
            userData = SpecialCookBoyMgr.getOrCreateUserData(getUserId());
            if (userData == null) {
                return;
            }
        }
        SpecialCookBoyProto.SpecialCookBoyUserDataMsg.Builder userDataMsg = parseUserDataToMsg(userData);
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_SYNC, userDataMsg);
    }

    /**
     * 转化个人信息为消息
     */
    public SpecialCookBoyProto.SpecialCookBoyUserDataMsg.Builder parseUserDataToMsg(SpecialCookBoyUserData userData) {
        SpecialCookBoyProto.SpecialCookBoyUserDataMsg.Builder userDataMsg = SpecialCookBoyProto.SpecialCookBoyUserDataMsg.newBuilder();
        for (Map.Entry<Integer, Integer> passMap : userData.getPassInfo().entrySet()) {
            SpecialCookBoyProto.SpecialCookBoyPassInfo.Builder passInfo = SpecialCookBoyProto.SpecialCookBoyPassInfo.newBuilder();
            passInfo.setLevelId(passMap.getKey());
            passInfo.setPassType(passMap.getValue());
            userDataMsg.addPassInfo(passInfo);
        }
        userDataMsg.setShareCode(userData.getShareCode());
        userDataMsg.addAllShareCodeUser(userData.getUsedOthersShareCode());
        List<Long> shareCodeUsePlayers = new ArrayList<>();
        for (Long shareCodeUseId : userData.getShareCodeUsePlayers()) {
            if (shareCodeUseId == null) {
                continue;
            }
            shareCodeUsePlayers.add(shareCodeUseId);
        }
        userData.setShareCodeUsePlayers(shareCodeUsePlayers);
        userDataMsg.addAllShareCodeUsedUser(userData.getShareCodeUsePlayers());
        userDataMsg.setCurrentLevelId(userData.getCurrentLevel());
        userDataMsg.setIsShare(userData.getGuideBit());
        userDataMsg.setGuideBit(userData.getGuideBit());
        userDataMsg.setLastRefreshShareCodeTime(userData.getShareCodeRefreshTime());
        userDataMsg.addAllHasOpenTipId(userData.getUnlockedTips());
        return userDataMsg;
    }


    /**
     * 开始游戏
     */
    public int startSmallGame(int nowLevel) {
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (userData == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        //先去更新一下关卡解锁
        checkLevelUnlock();
        //判断解锁map
        Map<Integer, Integer> passInfo = userData.getPassInfo();
        if (passInfo == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        if (!passInfo.containsKey(nowLevel)) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }
        if (passInfo.get(nowLevel) == 0) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_NO_UNLOCK;
        }
        userData.setIsShare(false);

        SpecialCookBoyProto.OpenSpecialCookBoySmallGameResp.Builder builder = SpecialCookBoyProto.OpenSpecialCookBoySmallGameResp.newBuilder();
        builder.setRet(0);
        builder.setUserData(parseUserDataToMsg(userData));
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_OPEN_GAME, builder);
        return 0;
    }

    /**
     * 分享加时间
     */
    public int shareAddTime(int level){
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (userData == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        SpecialCookBoyLevelConfig levelConfig = SpecialCookBoyMgr.getLevelConfig(level);
        if (levelConfig == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }

        //已经分享过
        if (userData.getIsShare()){
            return GameErrorCode.E_SPECIAL_COOK_BOY_HAS_SHARE;
        }

        SpecialCookBoyProto.ShareSpecialCookBoySmallGameResp.Builder builder = SpecialCookBoyProto.ShareSpecialCookBoySmallGameResp.newBuilder();
        userData.setIsShare(true);
        builder.setRet(0);
        builder.setUserData(parseUserDataToMsg(userData));
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_SHARE_ADD_TIME,builder);
        return 0;
    }

    /**
     * 使用提示道具
     */
    public int useTipsItem(int level,int tipId){
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        SpecialCookBoyConfig config = SpecialCookBoyMgr.getConfig();
        if (config == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }

        if (userData == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        SpecialCookBoyLevelConfig levelConfig = SpecialCookBoyMgr.getLevelConfig(level);
        if (levelConfig == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }


        Property cost = new Property();
        //写死12关不要消耗道具
        if (levelConfig.getLevelOrder() != 1 && levelConfig.getLevelOrder() != 2){
            cost.addProperty(config.getTipItemId(), BigInteger.ONE);
        }

        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)){
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        if (userData.getUnlockedTips().contains(tipId)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_HAS_UNLOCK_THIS_TIP;
        }
        SpecialCookBoyPromptConfig promptConfigByRecordId = SpecialCookBoyMgr.getSpecialCookBoyPromptConfigByRecordId(tipId);
        if (promptConfigByRecordId == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_THIS_TIP_NO_EXIST;
        }
        if (promptConfigByRecordId.getLevelId() != level){
            return GameErrorCode.E_SPECIAL_COOK_BOY_THIS_TIP_NO_THIS_LEVEL;
        }

        player.getModule(CurrencyModule.class).removeCurrency(cost,eLogMoneyType.SpecialCookBoy,eLogMoneyType.SpecialCookBoyUseTips);
        SpecialCookBoyProto.SpecialCookBoySmallGameUseTipResp.Builder builder = SpecialCookBoyProto.SpecialCookBoySmallGameUseTipResp.newBuilder();
        userData.addUnlockedTips(tipId);
        builder.setRet(0);
        builder.addAllHasOpenTipId(userData.getUnlockedTips());
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_USE_TIP_ITEM,builder);
        return 0;
    }


    /**
     * 结束游戏
     */
    public int endSmallGame(int nowLevel, int passType) {
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if(activityInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        if (passType != 1 && passType != 2) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_PARAM_ERROR;
        }

        if (userData == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        Map<Integer, Integer> passInfo = userData.getPassInfo();
        if (passInfo == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        if (!passInfo.containsKey(nowLevel)) {
            userData.setIsShare(false);
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }

        SpecialCookBoyLevelConfig levelConfig = SpecialCookBoyMgr.getLevelConfig(nowLevel);
        if (levelConfig == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_CONFIG_ERROR;
        }
        if (passInfo.get(nowLevel) == 0) {
            userData.setIsShare(false);
            return GameErrorCode.E_SPECIAL_COOK_BOY_LEVEL_NO_UNLOCK;
        }

        SpecialCookBoyProto.CloseSpecialCookBoySmallGameResp.Builder builder = SpecialCookBoyProto.CloseSpecialCookBoySmallGameResp.newBuilder();
        //已经通关过 或者没通过
        if (passInfo.get(nowLevel) == 2 || passType == 1) {
            builder.setRet(0);
            userData.setIsShare(false);
            builder.setUserData(parseUserDataToMsg(userData));
            player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_GAME_OVER, builder);
            if (passType == 1){ //未通关
                LogSpecialCookBoyFinishLevel log = new LogSpecialCookBoyFinishLevel(activityInfo.getActivityId(),getUserId(),nowLevel,levelConfig.getType(),0,"",new Date());
                AutoLogMgr.add(log);
            }else {
                LogSpecialCookBoyFinishLevel log = new LogSpecialCookBoyFinishLevel(activityInfo.getActivityId(),getUserId(),nowLevel,levelConfig.getType(),2,"",new Date());
                AutoLogMgr.add(log);
            }

        } else if (passInfo.get(nowLevel) == 1) {
            //通关
            passInfo.put(nowLevel, 2);
            userData.setUpdateOption();
            userData.setIsShare(false);
            String onceReward = levelConfig.getOnceReward();
            Property property = PropertyHelper.parseStringToProperty(onceReward);
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.SpecialCookBoy, eLogMoneyType.SpecialCookBoyGameOver);
            updateTaskValue();
            LogSpecialCookBoyFinishLevel log = new LogSpecialCookBoyFinishLevel(activityInfo.getActivityId(),getUserId(),nowLevel,levelConfig.getType(),1,onceReward,new Date());
            AutoLogMgr.add(log);

            builder.setRet(0);
            builder.setUserData(parseUserDataToMsg(userData));
            builder.setReward(PropertyHelper.parsePropertyToString(property));
            player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_GAME_OVER, builder);
        }
        syncUserData();
        return 0;
    }

    /**
     * 检测关卡解锁
     */

    public void checkLevelUnlock() {
        if (SpecialCookBoyMgr.getActivityInfo() == null) {
            return;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return;
        }
        int nowTask = player.getModule(TaskModule.class).getRealMainTaskId();
        int mainTaskIndex = TaskMgr.getMainTaskIndex(nowTask);
        SpecialCookBoyMgr.checkLevelUnlock(getUserId(), nowTask);
        if (userData == null){
            userData = SpecialCookBoyMgr.getUserData(getUserId());
        }
        if (userData != null) {
            userData.setCurrentLevel(mainTaskIndex);
        }

        syncUserData();
    }

    /**
     * 检测每日使用他人分享码次数重置
     */
    public void checkShareCodeUseCountReset() {
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null) {
            return;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return;
        }
        if (userData == null) {
            return;
        }
        long now = System.currentTimeMillis();
        if (DateHelper.isSameDay(userData.getLastResetUsedOthersShareCodeTime()/1000, now/1000)) {
            return;
        }
        //如果确实要刷新 邀请码也去查一下生成出来吧
        SpecialCookBoyMgr.getNewestShareCode(getUserId());
        userData.setUsedOthersShareCode(new ArrayList<>());
        userData.setLastResetUsedOthersShareCodeTime(now);
        syncUserData();
    }


    /**
     *使用分享码
     * @param shareCode
     * @return
     */

    public int useShareCode(String shareCode){
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        if (!ShareCodeUtil.checkStringAllInDigits(shareCode)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_ERROR;
        }
        if (shareCode.length() != 10){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_ERROR;
        }

        checkShareCodeUseCountReset();
        long targetUserId = SpecialCookBoyMgr.getUserIdByShareCode(shareCode);
        String userIdStr = String.valueOf(targetUserId);
        if (userIdStr.length() != 15) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_ERROR;
        }
        long targetServerId = Long.parseLong(userIdStr.substring(0, 8));
        List<Long> list = new ArrayList<>();
        list.add(0, targetServerId);
        list.add(1, targetUserId);
        ServerInfo serverInfo = ServerListMgr.getServerInfo(targetServerId);
        if (serverInfo == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_ERROR;
        }
        if (!ServerListMgr.isServerOpen(serverInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_SERVER_NO_OPEN;
        }
        long myServerId = GameServer.getInstance().getServerId();
        if (ServerListMgr.getChannelId(targetServerId) != ServerListMgr.getChannelId(myServerId)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_NO_SAME_CHANNEL;
        }
        if (userData.getUsedOthersShareCode().contains(shareCode)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_HAS_USE;
        }
        if (userData.getUsedOthersShareCode().size() >= SpecialCookBoyMgr.getConfig().getUseShareCodeMaxGetItem()){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_USE_TIME_LIMIT;
        }
        if (Objects.equals(userData.getShareCode(),shareCode)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_CAN_NO_USE_SELF;
        }

        //判断是否是同一个服
        if (targetServerId == myServerId){
            String language = player.getLanguage();
            int ret = SpecialCookBoyMgr.useShareCode(getUserId(), targetUserId, shareCode,language);
            if (ret != 0){
                return ret;
            }
            int dealUseCode = dealUseCode(shareCode);
            if (dealUseCode != 0){
                return dealUseCode;
            }
            syncUserData();
            return 0;

        }else { //不是同一个服的 丢到跨服去
            SpecialCookBoyProto.SpecialCookBoyUseCodeReqGameToCross.Builder builder = SpecialCookBoyProto.SpecialCookBoyUseCodeReqGameToCross.newBuilder();
            builder.setTargetServerId(targetServerId);
            builder.setTargetUserId(targetUserId);
            builder.setCode(shareCode);
            player.sendPacket(CrossProtocol.C_SPECIAL_COOK_BOY_USE_CODE_REQ_GAME_TO_CROSS,builder);
            return 0;
        }

    }

    public int dealUseCode(String shareCode){
        if (userData == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        List<String> othersShareCode = userData.getUsedOthersShareCode();
        if (othersShareCode.contains(shareCode)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_SHARE_CODE_HAS_USE;
        }
        othersShareCode.add(shareCode);
        Property property = PropertyHelper.parseStringToProperty(SpecialCookBoyMgr.getConfig().getUseCodeReward());
        player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.SpecialCookBoy, eLogMoneyType.SpecialCookBoyUseShareCodeReward);
        SpecialCookBoyProto.SpecialCookBoyUseShareCodeResp.Builder builder = SpecialCookBoyProto.SpecialCookBoyUseShareCodeResp.newBuilder();
        builder.setRet(0);
        builder.setReward(SpecialCookBoyMgr.getConfig().getUseCodeReward());
        player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_USE_SHARE_CODE,builder);


        return 0;
    }

    /**
     * 更新一下通关任务数据
     */
    public void updateTaskValue(){
        if (userData == null){
            return;
        }
        Map<Integer, Integer> passInfo = userData.getPassInfo();
        if (passInfo == null || passInfo.isEmpty()){
            return;
        }
        int count = 0;
        for (int passType : passInfo.values()){
            if (passType == 2){
                count++;
            }
        }
        //两个任务
        player.notifyListener(eGamePlayerEventType.SpecialCookBoyLevelMainRole.getValue(),count);
        player.notifyListener(eGamePlayerEventType.SpecialCookBoyLevelBeauty.getValue(),count);

    }

    /**
     * 看广告给提示
     */
    public int watchAdGetTipReward(int type ,int param){
        ActivityInfo activityInfo = SpecialCookBoyMgr.getActivityInfo();
        if (activityInfo == null) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!ActivityMgr.activityInTime(activityInfo)){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        if (!SystemOpenMgr.systemOpen(player,eSystemId.SpecialCookBoy.getValue())) {
            return GameErrorCode.E_SPECIAL_COOK_BOY_NO_UNLOCK;
        }
        SpecialCookBoyConfig config = SpecialCookBoyMgr.getConfig();
        if (config == null){
            return GameErrorCode.E_SPECIAL_COOK_BOY_ACTIVITY_NO_OPEN;
        }
        Map<Integer, SpecialCookBoyPromptConfig> promptConfigMap = config.getPromptConfigMap();
        //第一次 单个解锁
        if (type == 1){
            userData.addUnlockedTips(param);
        }else if (type == 2){
            //全部解锁
            for (SpecialCookBoyPromptConfig promptConfig : promptConfigMap.values()){
                if (promptConfig.getLevelId() == param){
                    userData.addUnlockedTips(promptConfig.getRecordId());
                }
            }
        }else {
            return GameErrorCode.E_ADVERTISEMENT_ACTIVITY_PARAM_ERROR;
        }
        syncUserData();
        return 0;

    }




}
